import { observable, action, toJS } from 'mobx';
import * as API from '../api/AccountSetting';
import {openLinkUrl} from '../util/IntegrationUtil';
import {Bubble} from 'weaver-mobile';

export default class AccountSetting {
  @observable paramObj = {};
  /**
   * 赋值方法
   */
  @action setState = (params={}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }
  /**
   * 获取form表单的信息
   */
  @action getOutterPCAndAppUrl = (params={}) => {
    API.getOutterPCAndAppUrl(params).then(result => {
      if(/\/integration\/accountSetting/gi.test(result.appUrl)) {
        let paramObj = {}, isOpenAutoLogin = "";
        if(Object.keys(result).includes("appUrl")) delete result.appUrl;
        if(Object.keys(result).includes("isOpenAutoLogin")) {
          isOpenAutoLogin = result.isOpenAutoLogin;
          delete result.isOpenAutoLogin;
        }
        if(Object.keys(result).includes("userName")) {
          paramObj.userName = result.userName;
        }
        if(Object.keys(result).includes("passWord")) {
          paramObj.passWord = result.passWord;
        }
        Object.keys(result).forEach(key => {
          if(["userName", "passWord"].indexOf(key) == -1) {
            paramObj[key] = result[key];
          }
        })
        if(isOpenAutoLogin != '1') {
          paramObj["logintype"] = result["logintype"] || "1";
        }
        this.paramObj = {...paramObj};
      }
    })
  }

  @action insertAccount = (sysId="", inteSwitch="", homeUrl="", openType="") => {
    let params = {sysid: sysId};
    Object.keys(toJS(this.paramObj)).forEach(key => {
      if(key != "appUrl") {
        params[`${key}_${sysId}`] = toJS(this.paramObj)[key];
      }
    })
    API.insertAccount(params).then(result => {
      if(result.appUrl) {
        if(inteSwitch == 1) {
          // 集成地址，直接跳转
          window.openLink.openInteApp({itemUrl: result.appUrl, inteSwitch: "1", openType: "2", inteSysid: sysId});
        }else {
          // 拼接集成参数
          let paramObj = "";
          try{
            Object.keys(toJS(this.paramObj)).forEach(key => {
              if(key) {
                paramObj += `&${key}=${toJS(this.paramObj)[key]}`;
              }
            })
          }catch(err) {}
          let param = '';
          if (result.appUrl && result.appUrl.indexOf('?') !== -1) {
            param = result.appUrl.slice(result.appUrl.indexOf('?') + 1);
          }
          let url = homeUrl;
          let split = '';
          if (/.*\?.*/.test(url)) {
            split = '&'
            if (/.*\?.*#\/(?!.*\?).*/.test(url)) {
              split = '?'
            }
          } else {
            split = '?'
          }
          if (param) {
            url += split + param + paramObj;
          } else {
            paramObj = paramObj.slice(1);
            url += split + paramObj;
          }
          window.openLink.openInteApp({itemUrl: url, inteSwitch: "0", openType: openType || "2", inteSysid: sysId});
        }
      }else{
        Bubble.error('请求出错，请检查', 1);
      }
    })
  }

  @action clearStore = () => {
    this.paramObj = {};
  }
}